/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Core.CoreManager;
import DataAccess.DocGia;
import DataAccess.Sach;
import java.util.List;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author My PC
 */
public class SachController extends CoreManager<Sach> {
    private String[] columnNames = {"Mã sách","Tên sách","Tác giả","Nhà xuất bản","Năm xuất bản","Thẻ loại","Ngôn ngữ","Số sách","Giá sách"};
    private String[] clmNames = {"MaSach","TenSach","TacGiaID","NhaXuatBanID","NamXuatBan","TheLoaiID","NgonNguID","SoSach","GiaSach"};
    TacGiaController tgControl=new TacGiaController();
    NhaXuatBanController nxbControl=new NhaXuatBanController();
    TheLoaiController tlControl=new TheLoaiController();
    NgonNguController nnControl=new NgonNguController();
    public SachController()
    { super();
    GetAllData("Sach");
    }
    @Override
    public void SetcolumnNames()
    {
        super.columnNames=columnNames;
    }
    public void showData(JTable table)
    {
        GetAllData("Sach");
        DefaultTableModel tableModel=new DefaultTableModel(columnNames, 0);
        table.setModel(tableModel);
       // int no=1;
        for(Object o: super.list)
        {
            Sach tg=(Sach)o;
            Vector oneRow=new Vector();
            oneRow.add(tg.getSachId());
            oneRow.add(tg.getTenSach());
            oneRow.add(tgControl.GetObjByID(tg.getTacGiaId(), "TacGia").getTenTacGia());
            oneRow.add(nxbControl.GetObjByID(tg.getNhaXuatBanId(), "NhaXuatBan").getTenNhaXuatBan());
            oneRow.add(tg.getNamXuatBan());
            oneRow.add(tlControl.GetObjByID(tg.getTheLoaiId(), "TheLoai").getTenTheLoai());
            oneRow.add(nnControl.GetObjByID(tg.getNgonNguId(), "NgonNgu").getTenNgonNgu());
            oneRow.add(tg.getSoSach());
            oneRow.add(tg.getGiaSach());
            tableModel.addRow(oneRow);
          //  System.out.println(tableModel.);
        }
    }
    public Sach getSachByIndex(int index)
    {
        return (Sach)list.get(index);
    }
    public JTable ThongKeSachHong()
    {
       String[] columnName={"Mã sách","Tên sách","Tác giả","Nhà xuất bản","Năm xuất bản","Thẻ loại","Ngôn ngữ","Số sách","Giá sách"};
        JTable tablename=new JTable();
         DefaultTableModel tableModel=new DefaultTableModel(columnName, 0);
        tablename.setModel(tableModel);
       // int no=1;
        List<Integer> lst=null;
          try
        {
            
            this.session.beginTransaction();
        this.query=this.session.createQuery("select SachID from ChiTietPhieuTra where TinhTrangSach = N'hư hỏng'");
        this.session.getTransaction().commit();
        lst=this.query.list();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        for(Object o: lst)
        {
            int i=Integer.parseInt(o.toString());
            Sach tg=GetObjByID(i, "Sach");
            Vector oneRow=new Vector();
            oneRow.add(tg.getSachId());
            oneRow.add(tg.getTenSach());
            oneRow.add(tgControl.GetObjByID(tg.getTacGiaId(), "TacGia").getTenTacGia());
            oneRow.add(nxbControl.GetObjByID(tg.getNhaXuatBanId(), "NhaXuatBan").getTenNhaXuatBan());
            oneRow.add(tg.getNamXuatBan());
            oneRow.add(tlControl.GetObjByID(tg.getTheLoaiId(), "TheLoai").getTenTheLoai());
            oneRow.add(nnControl.GetObjByID(tg.getNgonNguId(), "NgonNgu").getTenNgonNgu());
            oneRow.add(tg.getSoSach());
            oneRow.add(tg.getGiaSach());
            tableModel.addRow(oneRow);
          //  System.out.println(tableModel.);
        }
        return tablename;
    }
      public int GetLastID()
    {
        if(list.size()!=0)
        return ((Sach)this.list.get(list.size()-1)).getSachId();
        else return 0;
        
    }
      public void SearchObject( String[] keywords,JTable table)
    {
        boolean flag=true;
        List<Sach> lstObj;
        String str="from Sach where ";
        for(int i=1;i<keywords.length;i++)
            {
                if(keywords[i].length()!=0)
                {
                    if(!flag) str+=" and ";
                    str+=clmNames[i]+"='"+keywords[i]+"'";
                    flag=false;
                }
            }
        if(flag) str="from Sach";
        lstObj=GetData(str);
         DefaultTableModel tableModel=new DefaultTableModel(columnNames, 0);
        table.setModel(tableModel);
       // int no=1;
        for(Object o: lstObj)
        {
            Sach tg=(Sach)o;
            Vector oneRow=new Vector();
            oneRow.add(tg.getSachId());
            oneRow.add(tg.getTenSach());
            oneRow.add(tgControl.GetObjByID(tg.getTacGiaId(), "TacGia"));
            oneRow.add(nxbControl.GetObjByID(tg.getNhaXuatBanId(), "NhaXuatBan"));
            oneRow.add(tg.getNamXuatBan());
           oneRow.add(tlControl.GetObjByID(tg.getTheLoaiId(), "TheLoai"));
            oneRow.add(nnControl.GetObjByID(tg.getNgonNguId(), "NgonNgu"));
            oneRow.add(tg.getSoSach());
            oneRow.add(tg.getGiaSach());
            tableModel.addRow(oneRow);
        } 
            }
      public void ThongKeSachHong(JTable tablename)
      {
          DefaultTableModel tableModel=new DefaultTableModel(columnNames, 0);
        tablename.setModel(tableModel);
       // int no=1;
        for(Object o: super.list)
        {
            Sach tg=(Sach)o;
            Vector oneRow=new Vector();
            oneRow.add(tg.getSachId());
            oneRow.add(tg.getTenSach());
            oneRow.add(tgControl.GetObjByID(tg.getTacGiaId(), "TacGia"));
            oneRow.add(nxbControl.GetObjByID(tg.getNhaXuatBanId(), "NhaXuatBan"));
            oneRow.add(tg.getNamXuatBan());
            oneRow.add(tlControl.GetObjByID(tg.getTheLoaiId(), "TheLoai"));
            oneRow.add(nnControl.GetObjByID(tg.getNgonNguId(), "NgonNgu"));
            oneRow.add(tg.getSoSach());
            oneRow.add(tg.getGiaSach());
            tableModel.addRow(oneRow);
          //  System.out.println(tableModel.);
        }
      }
}
